package com.yeyks.appcontroller;

import com.yeyks.dal.domain.UserInfo;
import com.yeyks.dto.UserCouponInfoDTO;
import com.yeyks.dto.UserCouponResult;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.service.UserCouponService;
import com.yeyks.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author fuchangshuai
 * @date 2019/3/26 4:27 PM
 */
@Api(tags = "用户优惠券接口", description = "用户优惠券")
@Slf4j
@RestController("appUserCouponInfoController")
@RequestMapping(value = "/app/user/coupon")
public class UserCouponInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserCouponService userCouponService;

    /**
     * 根据一定条件查询当前用户的优惠券
     *
     * @param token
     * @param useStatus 优惠券状态, 默认1未使用，2已使用，3已过期
     * @param price     满减的价格
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "根据一定条件查询当前用户的优惠券", notes = "当需要获取满减券时传入price", response = UserCouponResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "useStatus", value = "优惠券状态, 默认1未使用，2已使用，3已过期", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "price", value = "满减的价格", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "当前页", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页个数", defaultValue = "10", dataType = "int")
    })
    @PostMapping(value = "/list")
    public ResponseEntity<?> couponList(@RequestParam(name = "token") String token,
                                        @RequestParam(name = "useStatus", defaultValue = "1") Integer useStatus,
                                        @RequestParam(name = "price", required = false) Integer price,
                                        @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        Map<String, Object> map = userCouponService.selectUserCouponList(userInfo.getId(), useStatus, price, pageNum, pageSize);
        return ResponseEntity.ok(AjaxResponseBody.success(map));
    }

    /**
     * 获取推广充值获取的优惠券
     *
     * @param token
     * @return
     */
    @ApiOperation(value = "获取推广充值获取的优惠券", notes = "根据body里数据判断，为null代表没有", response = UserCouponInfoDTO.class)
    @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String")
    @PostMapping(value = "/recommend/info")
    public ResponseEntity<?> couponList(@RequestParam(name = "token") String token) {
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        UserCouponInfoDTO userCouponInfoDTO = userCouponService.selectNewCoupon(userInfo);
        return ResponseEntity.ok(AjaxResponseBody.success(userCouponInfoDTO));
    }
}
